package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private RequestMappingHandlerMapping rmhm;

    @Autowired
    private PermissionMapper permissionMapper;
    

    public int deleteByPrimaryKey(Long id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    public int insert(Permission record) {
        return permissionMapper.insert(record);
    }

    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    public int updateByPrimaryKey(Permission record) {
        return permissionMapper.updateByPrimaryKey(record);
    }

    public PageResult query(QueryObject qo) {

        //1:查总条数
        int total = permissionMapper.queryForCount(qo);

        if(total == 0){
            return new PageResult();
        }
        //2:查分页数据
        List<Permission> rows = permissionMapper.queryForList(qo);

        return new PageResult(rows,total);
    }

    //加载系统权限
    public void reload() {
        //0:排重
        List<Permission> permissions = permissionMapper.selectAll();
        Set<String> set = new HashSet<>();
        for (Permission s : permissions) {
            set.add(s.getResource());
        }
        //通过请求的映射处理映射器
        //通过请求的映射处理映射器对象获取该对象所有的方法
        Map<RequestMappingInfo, HandlerMethod>  handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();

        for (HandlerMethod hm : values) {
            //遍历方法对象, 判断方法上面是否拥有RequiresPermissions注解
            RequiresPermissions ann = hm.getMethodAnnotation(RequiresPermissions.class);
            if(ann == null){
                continue;
            }
            //shiro权限注解value是数组,返回就是数组
            String resource = ann.value()[0];
            //如果已经存在跳过
            if(set.contains(resource)){
                continue;
            }
            //获取权限名称
            PermissionName pn = hm.getMethodAnnotation(PermissionName.class);
            String name  = pn.value();
            //如果解析注解,获取权限表达式, 然后封装成Permission对象然后保存
            Permission permission = new Permission();
            permission.setName(name);
            permission.setResource(resource);
            permissionMapper.insert(permission);
        }

    }

    public List<Permission> queryPermissionByRoleId(Long roleId) {

        return permissionMapper.queryPermissionByRoleId(roleId);
    }

    public List<String> queryPermissionResourceByEmpId(Long id) {

        return permissionMapper.queryPermissionResourceByEmpId(id);
    }


}
